package com.songtech.rcs.config.shiro;

import com.songtech.base.core.config.shiro.ApiShiroConfig;
import com.songtech.base.core.config.shiro.JWTFilter;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @Author: RenTie
 * @Date: 2018-09-01 10:19
 **/
@Configuration
public class NoTokenShiroConfig extends ApiShiroConfig{

    @Override
    public ShiroFilterFactoryBean getfilterFactoryBean() {
        ShiroFilterFactoryBean filterFactoryBean = new ShiroFilterFactoryBean();
        // 添加自己的过滤器并且取名为jwt
        Map<String, Filter> filterMap = new HashMap<>();
        filterMap.put("jwt", new NoTokenJWTFilter());
        filterFactoryBean.setFilters(filterMap);
        filterFactoryBean.setSecurityManager(getSecurityManager());
        filterFactoryBean.setUnauthorizedUrl("/401");


        /**
         *  只有在下面配置路径访问权限，Shiro才会执行自动跳转。
         *  如果使用Shiro注解权限，就只会报异常，
         *  就只能采用统一异常处理的方法。
         */
        //拦截器.
        Map<String, String> filterChainDefinitionMap = new LinkedHashMap<>();

        // 访问401和404页面不通过我们的Filter
        filterChainDefinitionMap.put("/401", "anon");

        // 所有api请求通过我们自己的JWT Filter
        filterChainDefinitionMap.put("/**", "anon");


        filterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);

        return filterFactoryBean;
    }
}
